package mtt.view.calendar;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;

import mtt.control.DisplayController;
import mtt.model.calendar.Day;
import mtt.model.calendar.Event;
import mtt.view.IMttDisplayAble;

/**
 * @author   hyphen
 */
public class DayView implements IMttDisplayAble {

	private static final Command commandBack = new Command("zurück", Command.BACK, 0);
	private static final Command commandOk = new Command("Anzeigen", Command.OK, 0);
	
	private static List list = new List("", List.IMPLICIT);

	private static final DayView dayView = new DayView();

	
	/**
	 * Factory-Methode liefert immer die gleiche DayView instanz
	 * @return DayView
	 */
	public static final DayView getInstance() {		
		return dayView;
	}

	/**
	 * enthält den aktuell angezeigten Tag
	 */
	private Day currentDay;
	
	private DayView() {		
		/*
		 * Einträge hinzufügen
		 */
		list.insert(Event.index_TIME_07To09, Event.getStringOfTimeId(Event.index_TIME_07To09), null);
		list.insert(Event.index_TIME_09To11, Event.getStringOfTimeId(Event.index_TIME_09To11), null);
		list.insert(Event.index_TIME_11To13, Event.getStringOfTimeId(Event.index_TIME_11To13), null);
		list.insert(Event.index_TIME_13To15, Event.getStringOfTimeId(Event.index_TIME_13To15), null);
		list.insert(Event.index_TIME_15To17, Event.getStringOfTimeId(Event.index_TIME_15To17), null);
		list.insert(Event.index_TIME_17To19, Event.getStringOfTimeId(Event.index_TIME_17To19), null);
		list.insert(Event.index_TIME_19To21, Event.getStringOfTimeId(Event.index_TIME_19To21), null);

		/*
		 * Buttons hinzufügen
		 */
		list.addCommand(commandBack);
		list.addCommand(commandOk);

		/*
		 * Listener hinzufügen
		 */
		list.setCommandListener(DisplayController.getInstance());
	}
	
	public void commandAction(Command command) {
		if (command == DayView.commandOk) {
			/*
			 * ausgewähltes Event anzeigen
			 */
			int indexOfEvent = list.getSelectedIndex();
			Event event = this.currentDay.getEventOfDay(indexOfEvent);
			DisplayController.getInstance().showEvent(event);
		}
	}

	public Displayable getDisplayable() {
		return list;
	}
	
	/**
	 * Initialisiert die Tagesansicht mit dem Übergebenen Tag
	 */
	public void initDayView(Day day) {
		this.currentDay = day;
		
		/*
		 * Liste anlegen
		 */
		list.setTitle("Tages-Übersicht: " + Day.getStringOfDayId(day.getIndexOfDay()));
		
	}

}
